package com.aiatomic.couting.widgets;

import java.util.ArrayList;
import java.util.List;

import com.aiatomic.couting.R;
import com.aiatomic.couting.models.TemplateEntity;
import com.aiatomic.couting.templates.ITemplateChangeEventListener;
import com.aiatomic.couting.templates.TemplateChangeEventDispatcher;
import com.aiatomic.couting.util.SystemUtil;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MyTemplateAdapter extends ArrayAdapter<TemplateEntity> implements ITemplateChangeEventListener {

	public MyTemplateAdapter(Context context, int resource, List<TemplateEntity> objects) {
		super(context, resource, objects);
		TemplateChangeEventDispatcher.getInstance().registListener(this);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		MyViewHolder holder = null;
		final TemplateEntity dataItem = this.getItem(position);
		if (null == convertView) {
			holder = new MyViewHolder();
			convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_my_template, null);
			convertView.setTag(holder);
		} else {
			holder = (MyViewHolder) convertView.getTag();
		}
		holder.txt_file_name = (TextView) convertView.findViewById(R.id.txt_file_name);
		holder.txt_file_desc = (TextView) convertView.findViewById(R.id.txt_file_desc);
		holder.txt_file_version = (TextView) convertView.findViewById(R.id.txt_file_version);
		holder.image_name = (ImageView) convertView.findViewById(R.id.image_name);
		holder.txt_file_name.setText(dataItem.getName());
		holder.txt_file_desc.setText(dataItem.getDesc());
		holder.txt_file_version.setText(String.format(getContext().getString(R.string.template_version),
				dataItem.getGroup_id() * 1000 + dataItem.getSb_template_id(), dataItem.getVersion()));
		// holder.image_name.setImageDrawable(drawable);
		DownloadImageTask task = new DownloadImageTask(holder.image_name, R.string.dir_cache_template_store);
		task.execute(dataItem.getPic());
		holder.addremove_btn = (Button) convertView.findViewById(R.id.addremove_btn);
		if (dataItem.isDownload()) {
			holder.addremove_btn.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.ic_template_delete));
		} else {
			holder.addremove_btn.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.ic_template_add));
		}
		holder.addremove_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				TemplateChangeEventDispatcher.getInstance().dispatchEvent(dataItem,
						TemplateChangeEventDispatcher.EVENT_CODE_REMOVE);
			}
		});
		return convertView;
	}

	class MyViewHolder {
		TextView txt_file_name;
		TextView txt_file_desc;
		TextView txt_file_version;
		ImageView image_name;
		Button addremove_btn;
	}

	@Override
	public void onTemplateChangeEvent(TemplateEntity entity, int eventCode) {
		switch (eventCode) {
		case TemplateChangeEventDispatcher.EVENT_CODE_ADD:
			List<TemplateEntity> objects = new ArrayList<TemplateEntity>();
			SystemUtil.getAllLocalStoreTemplates(getContext(), objects);
			if (objects.contains(entity)) {
				MyTemplateAdapter.this.add(entity);
				MyTemplateAdapter.this.notifyDataSetChanged();
			}
			break;
		case TemplateChangeEventDispatcher.EVENT_CODE_REMOVE:
			MyTemplateAdapter.this.remove(entity);
			MyTemplateAdapter.this.notifyDataSetChanged();
			break;
		}
	}
}
